﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using DCTLib.Common;
using DCTAccess.Business.Member;

public partial class Members_thay_doi_mat_khau : System.Web.UI.Page
{
    public string Message
    {
        get { return lblMsg.Text; }
        set { lblMsg.Text = value; }
    }
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    public bool ValidateData()
    {
        bool validated = true;
        if (ValidationHelper.IsEmpty(txtOldPassword.Text.Trim()))
        {
            Message += AppMessages.RequireField("Old Password") + "<br>";
            validated = false;
        }
        if (ValidationHelper.IsEmpty(txtNewPassword.Text.Trim()))
        {
            Message += AppMessages.RequireField("New Password") + "<br>";
            validated = false;
        }
        if (ValidationHelper.IsEmpty(txtReNewPassword.Text.Trim()))
        {
            Message += AppMessages.RequireField("Confirm New Password") + "<br>";
            validated = false;
        }
        else if (txtNewPassword.Text.Trim() != txtReNewPassword.Text.Trim())
        {
            Message += AppMessages.MustMatch("New Password", "Confirm New Password") + "<br>";
            validated = false;
        }
        return validated;
    }
    protected void btnSavePass_Click(object sender, EventArgs e)
    {
        if (ValidateData())
        {
            //int id = 
            Member mem = GetMemberForchangePass();
            Member memcm = MemberController.GetMemberByID(mem.ID);

            if (MD5Helper.GetMD5Hash(txtOldPassword.Text) == memcm.HashedPassword)
            {
                if (MemberController.ChangePassMember(mem))
                {
                    Message = AppMessages.UPDATE_SUCCESSFULLY;
                }
                else
                {
                    Message = AppMessages.CANNOT_UPDATE;
                }
            }
            else
            {
                Message = AppMessages.NOT_CORRECT_OLD_PASSWORD;
            }
        }
    }

    private Member GetMemberForchangePass()
    {
        Member mem = new Member();
        Member memInfo = MemberController.GetMemberByUsername(Session["User"].ToString());
        int id = memInfo.ID;        
        mem.ID = id;        
        mem.HashedPassword = MD5Helper.GetMD5Hash(txtNewPassword.Text);
        return mem;
    }

    protected void btnClearPass_Click(object sender, EventArgs e)
    {
        txtOldPassword.Text = "";
        txtNewPassword.Text = "";
        txtReNewPassword.Text = "";
    }
}
